/**
 * 
 */
package com.unsanctioned.radioagregator.share;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

/**
 * @author wizard
 *
 */
public class ShareIntentCreator {
	public static final String DOGTAG = ShareIntentCreator.class.getSimpleName(); 
	private static ShareIntentCreator instance = null;
	private Context context;
	/**
	 * Default constructor.
	 * @param context - a context from nearby activity.
	 */
	public ShareIntentCreator(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.instance = this;
	}
	public static ShareIntentCreator getInstance(Context context) {
		if (instance == null) return new ShareIntentCreator(context);
		else return instance;
	}
	/**
	 * Creates an intent specified to call Facebook intent.
	 * @param message - a message you want to share using your Facebook account.
	 * @return an intent.
	 */
	public Intent getForFacebook(String message) {
		Log.i(DOGTAG, "get for facebook intent");
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, Uri.parse(message).toString());
		PackageManager pm = context.getPackageManager();
		List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0);
		Log.i(DOGTAG, "activity to handle intent: " + activityList.size());
		if (activityList.size() > 0) {
			for (final ResolveInfo app : activityList) {
				Log.i(DOGTAG, "activityInfo.name: " + app.activityInfo.name);
			    if ((app.activityInfo.name).contains("facebook")) {
			        final ActivityInfo activity = app.activityInfo;
			        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
			        intent.addCategory(Intent.CATEGORY_LAUNCHER);
			        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
			        intent.setComponent(name);
			        break;
			   }
			}
		}
		return intent;
	}
	/**
	 * Creates an intent specified to call Twitter intent.
	 * @param message - a message you want to tweet using your Twitter account.
	 * @return
	 */
	public Intent getForTwitter(String message) {
		Log.i(DOGTAG, "get for twitter intent");
		Log.i("message",message);
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, message);
		PackageManager pm = context.getPackageManager();
		List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0);
		Log.i(DOGTAG, "activity to handle intent: " + activityList.size());
		if (activityList.size() > 0) {
			for (final ResolveInfo app : activityList) {
				Log.i(DOGTAG, "activityInfo: "+app.activityInfo.name);
			    if ("com.twitter.applib.PostActivity".equals(app.activityInfo.name)) {
			        final ActivityInfo activity = app.activityInfo;
			        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
			        intent.addCategory(Intent.CATEGORY_LAUNCHER);
			        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
			        intent.setComponent(name);
			        context.startActivity(intent);
			        break;
			   }
			}
		}			
		return intent;
	}
	public Intent getForSMS(String message) {
		Log.i(DOGTAG, "get for sms intent");
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"+""));
		intent.putExtra(Intent.EXTRA_TEXT, message);
		return intent;
	}
	public Intent getForGeneral(String message) {
		Log.i(DOGTAG, "get for general intent");
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, message);
		return intent;
	}
	/**
	 * Start an intent corresponding with your desire account/services and message 
	 * @param from - account of your choice, for now they are <b>Facebook</b>, <b>Twitter</b>, <b>SMS</b>, and <b>General</b>.
	 * @param message - a message you want to attach.
	 */
	public void startActivity(String from, String message) {
		if (from == "Facebook") {
			context.startActivity(getForFacebook(message));
		} else if (from == "Twitter") {
			context.startActivity(getForTwitter(message));
		} else if (from == "SMS") {
			context.startActivity(getForSMS(message));
		} else if (from == "Any") {
			context.startActivity(getForGeneral(message));
		}
	}
}
